﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using DataAccess;

namespace Business
{
    public class HangHoa
    {
        
        public int MaHang;
        public string MaHangHienThi 
        {
            get 
            {
                return "HH" + MaHang;
            }
        }
        public string TenHang { get; set; }
        public int MaLoai { get; set; }
        public string TenLoai { get; set; }
        public int SoLuongTon { get; set; }
        public double GiaVon { get; set; }
        public double GiaBan { get; set; }
        public string DonViTinh { get; set; }
        public string MoTa { get; set; }
        public bool TheoDoi { get; set; }
        

               
        public HangHoa(int maHang, string tenHang, int maLoai, string tenLoai, int soLuongTon,
            double giaVon, double giaBan, string donViTinh, string moTa, bool theoDoi)
        {
            this.MaHang = maHang;
            this.TenHang = tenHang;
            this.MaLoai = maLoai;

            this.TenLoai = tenLoai;

            this.SoLuongTon = soLuongTon;
            this.GiaVon = giaVon;
            this.GiaBan = giaBan;

            this.DonViTinh = donViTinh;
            this.MoTa = moTa;
            this.TheoDoi = theoDoi;
        }

        
        
        public HangHoa(string mahangHienThi, string tenHang, int maLoai, int soLuongTon,
            double giaVon, double giaBan, string donViTinh, string moTa, bool theoDoi)
        {
            this.MaHang = Int16.Parse(mahangHienThi.Substring(2));
            this.TenHang = tenHang;
            this.MaLoai = maLoai;
            this.SoLuongTon = soLuongTon;

            this.GiaVon = giaVon;
            this.GiaBan = giaBan;

            this.DonViTinh = donViTinh;
            this.MoTa = moTa;

            this.TheoDoi = theoDoi;
        }

       
          
        public HangHoa(string tenHang, int maLoai, int soLuongTon,
            double giaVon, double giaBan, string donViTinh, string moTa, bool theoDoi)
        {
            this.TenHang = tenHang;
            this.MaLoai = maLoai;

            this.SoLuongTon = soLuongTon;
            this.GiaVon = giaVon;

            this.GiaBan = giaBan;
            this.DonViTinh = donViTinh;
            this.MoTa = moTa;

            this.TheoDoi = theoDoi;
        }

        public static void ThemMoi(HangHoa nhapObject)
        {
            DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();

            DataAccess.HangHoa tamObject = new DataAccess.HangHoa();

            tamObject.TenHang = nhapObject.TenHang;

            tamObject.MaLoai = nhapObject.MaLoai;

            tamObject.SoLuongTon = nhapObject.SoLuongTon;
            tamObject.GiaVon = nhapObject.GiaVon;
            tamObject.GiaBan = nhapObject.GiaBan;
            tamObject.DonViTinh = nhapObject.DonViTinh;
            tamObject.MoTa = nhapObject.MoTa;

            tamObject.TheoDoi = nhapObject.TheoDoi;

            db.HangHoas.InsertOnSubmit(tamObject);
            db.SubmitChanges();
        }

        public static void CapNhat(HangHoa nhapObject)
        {
            DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();

            var tamObject = (from p in db.HangHoas
                                    where p.MaHang == nhapObject.MaHang
                                    select p).Single();

            tamObject.TenHang = nhapObject.TenHang;

            tamObject.MaLoai = nhapObject.MaLoai;
            tamObject.SoLuongTon = nhapObject.SoLuongTon;

            tamObject.GiaVon = nhapObject.GiaVon;
            tamObject.GiaBan = nhapObject.GiaBan;

            tamObject.DonViTinh = nhapObject.DonViTinh;
            tamObject.MoTa = nhapObject.MoTa;
            tamObject.TheoDoi = nhapObject.TheoDoi;

            db.SubmitChanges();
        }

        public static List<HangHoa> LayDanhSach()
        {
            DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();

            return (from p in db.HangHoas
                    select new HangHoa(p.MaHang, p.TenHang, p.MaLoai, p.LoaiHang.TenLoai, p.SoLuongTon, p.GiaVon, p.GiaBan, p.DonViTinh, p.MoTa, p.TheoDoi)).ToList();
        }

        public static HangHoa LayThongTinHang(int mahang)
        {
            DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();

            return (from p in db.HangHoas
                    where p.MaHang == mahang
                    select new HangHoa(p.MaHang, p.TenHang, p.MaLoai, p.LoaiHang.TenLoai, p.SoLuongTon, p.GiaVon, p.GiaBan, p.DonViTinh, p.MoTa, p.TheoDoi)).Single();
        }
        
    }
}
